package com.wsjc.web.controller.data;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.wsjc.common.core.constants.CommonConst;
import com.wsjc.common.core.page.PageBean;
import com.wsjc.common.core.page.PageParam;
import com.wsjc.common.core.utils.DateUtils;
import com.wsjc.common.core.utils.StringUtil;
import com.wsjc.manager.service.rpc.data.WjCollectionDataService;

@Controller
@RequestMapping(value = "/boss/data/")
public class WjDataController {

	@Autowired
	private WjCollectionDataService wjCollectionDataService ;
		
	private Logger logger = LoggerFactory.getLogger(WjDataController.class);
	
	
	/**
	 * 上传二进制数据包
	 * @param PartID
	 * @param Key
	 * @param FileName
	 * @param PDFFileName
	 * @param session
	 * @param error_info
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "searchData", method = RequestMethod.GET)
	public PageBean searchData(String dataType, String hospitalName, String hospitalType, String startTime, String endTime, String scope, String detectionResult,
			@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, Model model) {
		
		PageBean pageBean = null ;
		try {
			Map<String, Object> params = Maps.newHashMap();
			if(!StringUtil.isEmpty(hospitalName)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_HOSPITAL_NAME, hospitalName);
			}
			if(!StringUtil.isEmpty(hospitalType)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_HOSPITAL_TYPE, hospitalType);
			}
			if(!StringUtil.isEmpty(scope)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_SCOPE, scope);
			}
			if(!StringUtil.isEmpty(detectionResult)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_DETECTIONRESULT, detectionResult);
			}
			if(!StringUtil.isEmpty(startTime) && !StringUtil.isEmpty(endTime)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_START_TIME, String.valueOf(DateUtils.StringToDate(startTime).getTime()));
				params.put(CommonConst.SEARCH_DATA_CONDITION_END_TIME, String.valueOf(DateUtils.StringToDate(endTime).getTime()));
			}
			if(!StringUtil.isEmpty(hospitalType)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_HOSPITAL_TYPE, hospitalType);
			}

			//全国、省走solr , 市区 走db
			if(!StringUtil.isEmpty(scope)){
				String level = wjCollectionDataService.obtainScopeLevel(scope) ;
				if(level.equals(CommonConst.SCOPE_LEVEL_COUNTRY) || level.equals(CommonConst.SCOPE_LEVEL_PROVINCE)){
					pageBean = wjCollectionDataService.search(params, page, pageSize, dataType);
					model.addAttribute("pageBean", pageBean);
                    return pageBean ;
				}
			}
			
			PageParam pageParam = new PageParam(page,pageSize);
			pageBean = wjCollectionDataService.listDataPage(pageParam, params, scope, dataType);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		model.addAttribute("pageBean", pageBean);
		
		return pageBean ;
		
	}

							 
	




	
	
	
	
	
	
	
	
	

}
